
#include "LevelFix.h"

const unsigned char Hook70BinCode[] = {
    85, 139, 236, 81, 139, 81, 12, 139, 69, 4, 137, 69, 252, 131, 250, 85, 117, 7, 199, 65, 12,
    70,
    0, 0, 0, 141, 65, 8, 139, 229, 93, 195
};
const unsigned char WeakFix70BinCode[] = {
    85, 137, 229, 139, 1, 186, 181, 94, 25, 1, 255, 226, 204, 204, 204, 204, 85,
    137, 229, 139, 69, 8, 86, 139, 117, 12, 86, 80, 232, 223, 255, 255, 255, 199,
    69, 12, 0, 0, 0, 0, 139, 69, 4, 137, 69, 12, 129, 125, 12, 232, 18, 132, 2, 117,
    11, 131, 62, 70, 126, 6, 199, 6,
    70,
    0, 0, 0, 94, 93, 194, 8, 0
};
const unsigned char RedFixBinCode[] = {
    139, 69, 4, 137, 69, 12, 61, 232, 18, 132, 2, 117, 11, 131, 58, 70, 126, 6, 199, 2, 70, 0, 0, 0, 94, 93, 194, 8, 0
};


void MyWriteProcessMemory(HANDLE hProcess, unsigned long lpBaseAddress, unsigned long  lpBuffer,
                        SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten)
{
    WriteProcessMemory(hProcess, LPVOID(lpBaseAddress), LPCVOID(lpBuffer), nSize, lpNumberOfBytesWritten);
}

void InitLevelFix()
{
    // 等级补丁
    Hook hook;
    PVOID p = nullptr;

    p = VirtualAlloc(NULL, sizeof(Hook70BinCode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    memcpy(p, Hook70BinCode, sizeof(Hook70BinCode));
    hook.HookJmp((void *)4919056, p);

    p = VirtualAlloc(NULL, sizeof(WeakFix70BinCode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    memcpy(p, WeakFix70BinCode, sizeof(WeakFix70BinCode));
    hook.HookJmp((void *)18439856, p);

    // red fix
    p = VirtualAlloc(NULL, sizeof(RedFixBinCode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    memcpy(p, RedFixBinCode, sizeof(RedFixBinCode));
    hook.HookJmp((void *)18439949, p);

    p = VirtualAlloc(NULL, sizeof(RedFixBinCode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    memcpy(p, RedFixBinCode, sizeof(RedFixBinCode));
    hook.HookJmp((void *)18439968, p);

    // set level
    uchar levelCharArray[] = { 70 };
    MyWriteProcessMemory(GetCurrentProcess(), 4949591, (ulong)levelCharArray, 1, 0);
    MyWriteProcessMemory(GetCurrentProcess(), 8650103, (ulong)levelCharArray, 1, 0);
    MyWriteProcessMemory(GetCurrentProcess(), 9360631, (ulong)levelCharArray, 1, 0);
    MyWriteProcessMemory(GetCurrentProcess(), 9542578, (ulong)levelCharArray, 1, 0);
}
